package bs.jizhang.controller;

import bs.jizhang.model.AmountPlatform;
import bs.jizhang.repository.AmountPlatformRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/amountPlatform")
public class AmountPlatformController {

    @Autowired
    private final AmountPlatformRepository AmountPlatformRepository;


    public AmountPlatformController(AmountPlatformRepository amountPlatformRepository) {
        AmountPlatformRepository = amountPlatformRepository;
    }

    // 获取分类列表（支持分页和排序）
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> list(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "id,asc") String sort) {
        Map<String, Object> response = new HashMap<>();
        Map<String, Object> data = new HashMap<>();

        // 解析排序参数
        String[] sortParams = sort.split(",");
        Sort.Direction direction = Sort.Direction.fromString(sortParams[1]);
        Sort sortBy = Sort.by(direction, sortParams[0]);

        // 创建分页对象
        Pageable pageable = PageRequest.of(page, size, sortBy);

        // 查询分页数据
        Page<AmountPlatform> AmountPlatform = AmountPlatformRepository.findAll(pageable);

        // 构建响应
        data.put("list", AmountPlatform.getContent());
        data.put("totalElements", AmountPlatform.getTotalElements());
        data.put("totalPages", AmountPlatform.getTotalPages());
        data.put("currentPage", AmountPlatform.getNumber());
        data.put("pageSize", AmountPlatform.getSize());
        data.put("hasNextPage", AmountPlatform.hasNext());

        response.put("code", 0);
        response.put("data", data);

        return ResponseEntity.ok(response);
    }

    // 获取分类详情
    @GetMapping("/get/{id}")
    public ResponseEntity<Map<String, Object>> get(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        AmountPlatform AmountPlatform = AmountPlatformRepository.findById(id).orElse(null);
        if (AmountPlatform != null) {
            response.put("status", 0);
            response.put("msg", "Category details retrieved successfully");
            response.put("data", AmountPlatform);
        } else {
            response.put("status", 1);
            response.put("msg", "Category not found");
        }
        return ResponseEntity.ok(response);
    }

    // 添加分类
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> add(@RequestBody AmountPlatform AmountPlatform) {
        Map<String, Object> response = new HashMap<>();
        AmountPlatform savedAmountPlatform = AmountPlatformRepository.save(AmountPlatform);
        response.put("status", 0);
        response.put("msg", "AmountPlatform added successfully");
        response.put("data", savedAmountPlatform);
        return ResponseEntity.ok(response);
    }

    // 修改分类
    @PutMapping("/update/{id}")
    public ResponseEntity<Map<String, Object>> update(@PathVariable Long id, @RequestBody AmountPlatform AmountPlatform) {
        Map<String, Object> response = new HashMap<>();
        if (AmountPlatformRepository.existsById(id)) {
            AmountPlatform.setId(id);
            AmountPlatform updatedAmountPlatform = AmountPlatformRepository.save(AmountPlatform);
            response.put("status", 0);
            response.put("msg", "Category updated successfully");
            response.put("data", AmountPlatform);
        } else {
            response.put("status", 1);
            response.put("msg", "Category not found");
        }
        return ResponseEntity.ok(response);
    }

    // 删除分类
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        if (AmountPlatformRepository.existsById(id)) {
            AmountPlatformRepository.deleteById(id);
            response.put("status", 0);
            response.put("msg", "Category deleted successfully");
        } else {
            response.put("status", 1);
            response.put("msg", "Category not found");
        }
        return ResponseEntity.ok(response);
    }

}